﻿Imports System.Data.SqlClient
Public Class 财务盘点
    Private connStr As String = DbConnectionHelper.GetConnectString

    Private Sub 财务盘点_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
        getNowYe()
    End Sub
    Private Sub getNowYe()
        ' 2️⃣ 建立连接对象
        Using conn As New SqlConnection(DbConnectionHelper.GetConnectString())
            Try
                ' 打开连接
                conn.Open()

                ' 3️⃣ 创建 SqlCommand 对象，指定要执行的存储过程名称
                Using cmd As New SqlCommand("usp_GetWdye", conn)
                    cmd.CommandType = CommandType.StoredProcedure  ' 告诉系统这是个存储过程

                    ' 4️⃣ 执行存储过程并获取结果（存储过程里有 SELECT）
                    Using reader As SqlDataReader = cmd.ExecuteReader()

                        ' 5️⃣ 判断是否有返回数据
                        If reader.HasRows Then
                            While reader.Read()
                                txtYJE.Text = reader("金额")
                            End While
                        Else
                            MessageBox.Show("系统未设置期初！", "提示")

                        End If
                    End Using
                End Using
            Catch ex As Exception
                LogHelper.WriteLog("获取我的余额错误：" & ex.Message)
                MessageBox.Show("执行出错：" & ex.Message)
            End Try
        End Using
    End Sub
    Private Sub LoadData()
        Try
            Using conn As New SqlConnection(connStr)
                Using cmd As New SqlCommand("usp_GetPdjlData", conn)
                    cmd.CommandType = CommandType.StoredProcedure

                    Dim da As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    da.Fill(dt)
                    dgvPD.DataSource = dt

                    ' 自动适配列宽
                    dgvPD.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                    dgvPD.Columns("备注").FillWeight = 150
                End Using
            End Using
        Catch ex As Exception
            LogHelper.WriteLog("加载盘点记录失败：" & ex.Message)
            MessageBox.Show("加载盘点记录失败：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        ' 1️⃣ 校验输入
        If txtYJE.Text = "" Or txtMJE.Text = "" Then
            MessageBox.Show("请填写完整的金额！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If

        Try
            ' 2️⃣ 连接数据库
            Using conn As New SqlConnection(connStr)
                conn.Open()

                ' 3️⃣ 创建 SqlCommand，调用存储过程 usp_AddNewPdData
                Using cmd As New SqlCommand("usp_AddNewPdData", conn)
                    cmd.CommandType = CommandType.StoredProcedure

                    ' 4️⃣ 传入参数
                    cmd.Parameters.AddWithValue("@yje", Val(txtYJE.Text))
                    cmd.Parameters.AddWithValue("@mje", Val(txtMJE.Text))
                    cmd.Parameters.AddWithValue("@memo", txtMemo.Text)

                    ' 5️⃣ 执行存储过程并读取返回消息
                    Dim result As String = ""
                    Using reader As SqlDataReader = cmd.ExecuteReader()
                        If reader.Read() Then
                            result = reader("Msg").ToString()
                        End If
                    End Using

                    ' 6️⃣ 显示执行结果
                    MessageBox.Show(result, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Using
            End Using

            ' 7️⃣ 刷新数据
            LoadData()

        Catch ex As Exception
            LogHelper.WriteLog("盘点执行出错：" & ex.Message)
            MessageBox.Show("执行出错：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnVoid_Click(sender As Object, e As EventArgs) Handles btnVoid.Click
        If dgvPD.SelectedRows.Count = 0 Then
            MessageBox.Show("请先选择要作废的记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If
        Dim id As Integer = dgvPD.SelectedRows(0).Cells("ID").Value
        Using conn As New SqlConnection(connStr)
            conn.Open()
            Dim cmd As New SqlCommand("UPDATE CWPDJLK SET JLZT=0, MEMO=MEMO+'[作废]' WHERE ID=@ID", conn)
            cmd.Parameters.AddWithValue("@ID", id)
            cmd.ExecuteNonQuery()
        End Using
        LoadData()
        MessageBox.Show("该记录已作废！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class
